package com.hjx.controller;

import com.hjx.entity.SysRole;
import com.hjx.entity.SysUserRole;
import com.hjx.logging.LogAnnotation;
import com.hjx.response.AjaxResponse;
import com.hjx.response.ResponseType;
import com.hjx.service.SysUserRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2020/6/16 - 14:10
 */
@RestController
@RequestMapping("ur")
public class SysUserRoleController {
    @Resource
    SysUserRoleService sysUserRoleService;


    //添加之前先根据uid查询目标用户没有的角色 进行回显
    @LogAnnotation
    @GetMapping("/findNoGetRoleByUid/{uid}")
    public AjaxResponse findNoGetRoleByUid(@PathVariable String uid) {
        List<SysRole> roles = sysUserRoleService.findNoGetRoleByUid(uid);
        if (roles!=null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, roles);
        }
        return new AjaxResponse(1, "未查询到数据", null);
    }

    @LogAnnotation
    @PostMapping("/insertBatch")
    public AjaxResponse insertBatch(@RequestBody SysUserRole sysUserRole) {
        boolean b = sysUserRoleService.insertBatch(sysUserRole);
        if (b) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

    //删除之前先根据要添加的用户的UID进行查询当前已有的角色
    @LogAnnotation
    @GetMapping("/findRoleByUid/{uid}")
    public AjaxResponse findRoleByUid(@PathVariable String uid) {
        List<SysRole> roles = sysUserRoleService.findRoleByUid(uid);
        if (roles!=null) {
            return new AjaxResponse(ResponseType.QUERY_SUCCESS, roles);
        }
        return new AjaxResponse(ResponseType.QUERY_FAILED);
    }

    //删除
    @LogAnnotation
    @PostMapping("/delete")
    public AjaxResponse delete(@RequestBody SysUserRole sysUserRole) {
        boolean result = sysUserRoleService.delete(sysUserRole);
        if (result) {
            return new AjaxResponse(ResponseType.OPERATION_SUCCESS);
        }
        return new AjaxResponse(ResponseType.OPERATION_FAILED);
    }

}
